<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script>

var hasPathSum = function(root, sum) {
    let flag = false
    let dfs = (node,res) => {
        res = res + node.val
        if(!node.left && !node.right){
            if(res == sum){
                flag = true
            }
        }
        if(node.left) dfs(node.left,res)
        if(node.right) dfs(node.right,res)
    }
    if(root){
        dfs(root,0)
    }
    return flag
};

</script>
</body>
</html>